// pages/mine/mine.js
const utils = require('../../utils/util.js')
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    startDateDisable: utils.formatDate(new Date),
    date: '请选择',
    time: '请选择',
    sex: '请选择',
    name: '',
    mobile: '',
    id: 0,
    info: {},
    housing_types: {
      1: '小区住宅',
      2: '公寓'
    },
    room_type: {
      1: '押一付一',
      2: '押一付二',
      3: '押一付三',
      4: '押二付一',
      5: '押二付二',
      6: '押二付三',
      7: '半年付',
      8: '年付'
    },
    imgDomain: app.data.imgDomain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData(options.id)
    this.setData({id: options.id})
  },
  /**
   * 提交数据
   */
  submit () {
    if (!this.data.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return false
    }

    if (!this.data.sex || this.data.sex === '请选择') {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return false
    }
    if (!this.data.mobile) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
      return false
    }
    if (!(/^1[3|4|5|6|7|8|9]\d{9}$/).test(this.data.mobile)) {
      wx.showToast({
        title: '手机号码格式错误',
        icon: 'none'
      })
      return false
    }
    if (!this.data.date || this.data.date === '请选择') {
      wx.showToast({
        title: '请选择参观日期!',
        icon: 'none'
      })
    }
    if (!this.data.time || this.data.date === '请选择') {
      wx.showToast({
        title: '请选择参观时间',
        icon: 'none'
      })
    }
    wx.showLoading()
    app.request('post', 'wholeAppointment', {
      name: this.data.name,
      mobile: this.data.mobile,
      date: this.data.date,
      time: this.data.time,
      sex: this.data.sex,
      whole_id: Number(this.data.id)
    }, (res, msg) => {
      wx.showModal({
        title: '预约成功',
        content: msg,
        success: (res) => {
          if (res.confirm) {
            wx.navigateBack({
              delta: 2
            })
          }
        }
      })
    })
  },
  getData(id) {
    wx.showLoading()
    app.request('get', 'infoById', { id: id }, (r) => {
      this.setData({ info: r, banner: r.slider })
    })
  },
  bindMobileChange (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  bindNameChange (e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindSexChange: function (e) {
    this.setData({
      sex: Number(e.detail.value) + 1
    })
  }
})